// template.js you can generate
module.exports = {
    indexTemplate: (dbName) => {
        return `'use strict';
const uniID = require('uni-id');
const proParams = require('processing-parameters');
const db = uniCloud.database();
const dbCmd = db.command;
const collection = db.collection('${dbName}');
exports.main = async (event, context) => {
    let datas = proParams(event)
    let params = ''
    if(datas.data.params){
        params = datas.data.params
    }
    let action = datas.data.action
    let token = datas.event.headers.uniidtoken
    let type = require(\`./modules/\${action}.js\`)
    return type({collection,dbCmd,params,token,uniID})
};
`
    },
    addTemplate: () => {
        return `'use strict';
module.exports = async({collection,params,token,uniID})=>{
    let res = {}
    let payload = await uniID.checkToken(token)
    if(payload.code == 0){
        res = await collection.add(params)
        res = {...res,code:0,msg:'添加成功'}
    }else{
        res = payload
    }
    //返回数据给客户端
    return res
}`
    },
    delTemplate: () => {
        return `'use strict';
module.exports = async({collection,params,token,uniID})=>{
    let res = {}
    let payload = await uniID.checkToken(token)
    if(payload.code == 0){
        res = await collection.doc(params._id).remove();
        res = {...res,code:0,msg:'删除成功'}
    }else{
        res = payload
    }
    //返回数据给客户端
    return res
}`
    },
    updateTemplate: () => {
        return `'use strict';
module.exports =  async({collection,params,token,uniID})=>{
    let res = {}
    let payload = await uniID.checkToken(token)
    if(payload.code == 0){
        let id = params._id
        delete params._id
        res = await collection.doc(id).update(params)
        res = {...res,code:0,msg:'更新成功'}
    }else{
        res = payload
    }
    //返回数据给客户端
    return res
}`
    },
    listTemplate: (columns) => {
        return `'use strict';
module.exports = async ({collection, dbCmd, params, token, uniID}) => {
	let res = {}
	let payload = await uniID.checkToken(token)
	if(params){
		params = JSON.parse(params)
	}
	if (payload.code == 0) {
			let total = (await collection.where({_id: dbCmd.exists(true)}).count()).total
		if(params) {
			let pageSize = Number(params.pageSize)
			let pageNumber = Number((Number(params.pageNumber)-1) * pageSize)
			res = await collection.where({${columns}})
				.skip(pageNumber)
				.limit(pageSize)
				.get()
		}else{
			res = await collection.get()
		}
		res = {...res,total,code:0,msg:'查询成功'}
	} else {
		res = payload
	}
	//返回数据给客户端 
	return res
}
`
    },
    packageTemplate: (name) => {
        return `{
    "name": "${name}",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \\"Error: no test specified\\" && exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
        "processing-parameters": "file:../common/processing-parameters",
        "uni-id": "file:../common/uni-id"
    }
}`
    }

}
